<template>
	<view>
		<view class="search">
			<view class="searchBox">
				<navigator url="#">
					<image src="../../static/image/search.png" mode=""></image>
					搜索全部
				</navigator>
			</view>
		</view>

		<view class="cate">
			<view class="cateleft">
				<scroll-view class="cateNameList" scroll-y="true" :scroll-top="scrollTop">
					<view class="cateNameItem" 
					:class="{cateActive: currentIndex === index}" 
					v-for="(item, index) in cate" :key="index" @click="handle(index)">
						{{item.catename}}
						<view class="cateLine"></view>
					</view>
					
				</scroll-view>
			</view>
			
			<view class="cateright">
				<scroll-view class="cateRightScroll" scroll-y="true" 
							:scroll-into-view="'cate' + mainIndex"
							scroll-with-animation="true" @scroll="rigthScroll">
					<view class="cateItem" v-for="(item, index) in cate" :key="index" 
						:id="'cate' + index">
						<view class="caterightTitle">
							——<text>{{item.catename}}</text>——
						</view>
						<view class="cateRightList">
							<view class="cateRightItem" v-for="(child, childIndex) in item.product" 
								:key="childIndex">
								<navigator hover-class="none" :url="'../detail/detail?id='+child.id">
									<image :src="imgUrl + child.mainimage" mode=""></image>
									<text>{{child.smalltitle}}</text>
								</navigator>
							</view>
						</view>
					</view>
					
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cate:[],
				currentIndex:0,
				imgUrl:this.$imgUrl,
				rectInfo:[],
				mainIndex:0,
				scrollTop:0
			}
		},
		onLoad() {
			this.getCate()
		},
		mounted(){
			setTimeout(()=>{
				this.getRectInfo()
				console.log(this.rectInfo)
				console.log(this.rectInfo.length)
			},200)
		},
		methods: {
			getCate(){
				uni.request({
					url:this.apiUrl + '/index/cate',
					success:(res)=>{
						// console.log(res)
						let {data} = res.data
						this.cate = data
						console.log(this.cate)
					}
				})
			},
			// 监听滚动事件   与获取矩形信息（节点信息 ）联动左侧边栏动态改变
			rigthScroll(e){
				console.log(e.detail.scrollTop)
				let scrollTop = e.detail.scrollTop
				for(let i = 0; i < this.rectInfo.length; i++){
					if(scrollTop > this.rectInfo[i].top && scrollTop < this.rectInfo[i].bottom){
						this.currentIndex = i
						this.scrollTop = i*uni.upx2px(100)
					}
				}
			},
			// 点击菜单栏
			handle(index){
				this.currentIndex = index
				this.mainIndex=index
				// console.log(index*50)
				this.scrollTop = index*uni.upx2px(100) // rpx转upx
			},
			//获取矩形信息（节点信息 ）
			getRectInfo(){
				var top=0;
				var bottom=0;
				var temp=0;
				for(var i=0;i<this.cate.length;i++)
				{
					
					let view = uni.createSelectorQuery().in(this).select("#cate"+i);
					
					view.fields({
					  size: true,
					  rect: true
					}, data => {
						
					  top=temp;
					  bottom=top+data.height
					  temp+=data.height;
					  // 将数据存储到自定义的一个数组
					  this.rectInfo[i]={'top':top,"bottom":bottom}
					console.log(top, bottom)
					}).exec();
				}
			}
		}
	}
</script>

<style>
	page {
		background: #f7f7f7;
	}

	.search {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 110rpx;
		width: 100%;
		background: #fff;
	}

	.searchBox {
		width: 690rpx;
		height: 70rpx;
		font-size: 28rpx;
		border-radius: 10rpx;
		color: #999;
		background-color: #f7f7f7;
	}

	.searchBox navigator {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 70rpx;
	}

	.searchBox image {
		width: 38rpx;
		height: 38rpx;
		margin-right: 10rpx;
	}

	.cate {
		position: absolute;
		/* background: red; */
		width: 100%;
		top: 120rpx;
		bottom: 0;
		display: flex;
	}

	.cateleft {
		width: 200rpx;
		height: 100%;
		background: #f7f7f7;
	}

	.cateNameList {
		height: 100%;
		overflow: auto;/* auto会有滑动效果 hidden没有 */
	}

	.cateNameItem {
		font-size: 28rpx;
		color: #000;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		position: relative;
	}

	.cateActive {
		background: #fff;
	}

	.cateLine {
		position: absolute;
		width: 4rpx;
		height: 30rpx;
		top: 35rpx;
		background: #f7f7f7f;
	}

	.cateActive .cateLine {
		background: #0086B3;
	}

	.cateright {
		width: 550rpx;
		background: #fff;
		height: 100%;
	}

	.cateRightScroll {
		height: 100%;
		overflow: auto;
	}

	.caterightTitle {
		line-height: 86rpx;
		padding-top: 16rpx;
		color: #999;
		font-size: 28rpx;
		text-align: center;
	}
	.caterightTitle text{padding:0 30rpx}
	.cateRightList{
		display: flex;
		flex-wrap: wrap;
		
	}
	.cateRightItem{
		width: 33.3%;
		margin-top: 20rpx;
	}
	.cateRightItem image{
		width: 160rpx;
		height: 160rpx;
		display: block;
		margin: 0 auto;
	}
	.cateRightItem text{
		line-height: 36rpx;
		font-size: 24rpx;
		text-align: center;
		display: block;
		width: 100%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap; // 不换行
	}
</style>
